package com.vedio.mapperImp;

import com.vedio.entity.User;
import com.vedio.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserMapperImp {
    public UserMapperImp() {
    }

    public List<User> selectAll() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.获取SqlSessionFactory对象，用它执行sql语句
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3.执行sql
        //List<Product> products = sqlSession.selectList("test.selectAll");
        //获取mapper接口的代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //执行方法
//        List<Product> products = productMapper.selectByCondition(product);
        List<User> users = userMapper.selectAll();
        //释放资源
        sqlSession.close();
        return users;
    }
}
